import axios from 'axios'
import config from '/config'

const request = axios.create({
    baseURL: '',
    timeout: 5000,
})
request.httpUrl = config.httpUrl //注入
//请求拦截器
request.interceptors.request.use(
    (config) => {
        //处理数据

        let option = config.data ? 'data' : 'params'
        for (let i in config[option]) {
            //去除空格
            if (typeof config[option][i] === 'string') config[option][i] = config[option][i].trim()
            //空值移除
            if (!config[option][i] && config[option][i] !== 0 && typeof config[option][i] !== 'boolean') delete config[option][i]
            // 去除所有$开头字段
            if (/^\$/.test(i)) delete config[option][i]
        }
        // 设置请求头
        /*if (store.getters.token) {
            config.headers["X-Token"] = getToken();
            config.headers["Content-Type"] = "application/json";
        }*/
        //url为http或者https完整链接路径开头直接请求不拼接baseURL
        if (/(http|https):\/\/([\w.]+\/?)\S*/.test(config.url)) config.baseURL = ''
        if (config.loading) {
            window.$app.$toast.loading({
                message: '请稍等...',
                forbidClick: true,
                duration: 0,
            })
        }

        console.log(config.apiName, {
            请求参数: config,
        })
        return config
    },
    (error) => {
        return Promise.reject(error)
    }
)

request.interceptors.response.use(
    (response) => {
        window.$app.$toast.clear()
        console.log('响应参数', response)
        switch (response.data.code) {
            case 200:
                return {...response.data, data: response.data.data}
            case 404: //暂无权限
                window.$app.$notify.warning({
                    title: '404 资源不存在',
                    duration: 5 * 1000,
                })
                return new Object(null)
            case 500:
                window.$app.$notify.error({
                    title: '500',
                    message: `服务器错误`,
                    duration: 5 * 1000,
                })
                return new Object(null)
            default:
                window.$app.$notify.error({
                    title: '失败 ' + 'code:' + response.data.code,
                    message: response.data.msg,
                    duration: 5 * 1000,
                })
                return Promise.resolve(response.data.msg || 'Error')
        }
    },
    (error) => {
        const {
            data: {code, msg},
        } = error.response

        if (code === -2) {
            window.$app.$router.push('/login')
            window.$app.$message.error('登录失效,重新登录')
        } else {
            window.$app.$message.error(msg)
        }
        window.$app.$toast.clear()
        return Promise.reject(error)
    }
)

request.get = (options) =>
    request({
        method: 'get',
        ...options,
    })
request.post = (options) =>
    request({
        method: 'post',
        ...options,
    })
request.del = (options) =>
    request({
        method: 'del',
        ...options,
    })
request.put = (options) =>
    request({
        method: 'put',
        ...options,
    })

export default request
